package com.trade.dao.impl;

import java.util.List;

import com.trade.common.dao.impl.GenericDao;
import com.trade.dao.IUserDao;
import com.trade.model.User;

/**
 * 
 * @author Lin Yang
 * 
 */
public class UserDao extends GenericDao<User, Long> implements IUserDao {

    @Override
    public User getByUserName(String userName) {
        List<User> users = find("from User where loginName=?", userName);
        if (users == null || users.size() == 0) {
            return null;
        }
        return users.get(0);
    }
    
    @Override
    public Boolean AddUser(User user)
    {
    	try {
    		List<User> users = find("from User where LoginName=?", user.getLoginName());
    		if(users!=null&&users.size()>0)
    		{
    			return false;
    		}
    		else
    		{
    			save(user);
    		}
    	} catch(Exception ex) {
    		return false;	
    	}
        return true;
    }
    
    @Override
    public Boolean UpdateUser(User user)
    {
    	try {
    		update(user);
    	} catch(Exception ex) {
    		return false;	
    	}
        return true;
    }
    
    @Override
    public List<User> getAllUsers()
    {
    	List<User> users = find("from User",null);
        if (users == null || users.size() == 0) {
            return null;
        }
        return users;
    }
    

    @Override
    public Boolean DeleteUser(String strUserID)
    {
    	try {
    		List<User> users = find("from User where userid=?", strUserID);
    		delete(users.get(0));
    	} catch(Exception ex) {
    		return false;	
    	}
        return true;
    }
    
    @Override
   public User GetUserById(String strUserID)
    {
         List<User> users = find("from User where userid=?", strUserID);
         if (users == null || users.size() == 0) {
             return null;
         }
         return users.get(0);
    }

}
